﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FifaChamps.API.Data;

namespace FifaChamps.API.Data
{
    public static partial class DataManager
    {
        public static int Add_Player(string name, string surname, string email, string phone, string nickname, string online, int? firstteam, int? secondteam, string playerpicture, DateTime? joindate, bool? registered) 
        {
            using (var db = DataManager.FifaDB())
            {
                var result = db.SP_Add_Player(name, surname, email, phone, nickname, online, firstteam, secondteam, playerpicture, joindate, registered).First();
                return result.PlayerID.Value;
            }
        }

        public static int Update_Player(int playerid, string name, string surname, string email, string phone, string nickname, string online, int? firstteam, int? secondteam, string playerpicture) 
        {
            using (var db = DataManager.FifaDB())
            {
                var result = db.SP_Update_Player(playerid, name, surname, email, phone, nickname, online, firstteam, secondteam, playerpicture).First();
                return result.PlayerID.Value;
            }
        }

        public static Player Get_Player(int playerid)
        {
            using (var db = DataManager.FifaDB())
            {
                var result = db.FCT_Players.Where(player => player.ID == playerid).FirstOrDefault();

                if (result == null) throw new NullReferenceException(string.Format("Player with ID {0} cannot be found", playerid));

                return Player.Factory(result.ID, result.Name, result.Surname, result.Email, result.Phone, result.Nickname, result.OnlinePlayer, result.FirstTeam, result.SecondTeam, result.PlayerPicture, result.JoinDate, result.Registered);
            }
        }

        public static List<Player> Get_Players()
        {
            List<Player> players= new List<Player>();

            using (var db = DataManager.FifaDB())
            {
                var results = db.FCT_Players.ToList();
                
                foreach (var result in results)
                {
                    players.Add(Player.Factory(result.ID, result.Name, result.Surname, result.Email, result.Phone, result.Nickname, result.OnlinePlayer, result.FirstTeam, result.SecondTeam, result.PlayerPicture, result.JoinDate, result.Registered));
                }
            }

            return players;
        }
    }
}
